#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>



// my functions
#include <open_stream_name_with_subscript.h>
#include <print_error.h>
#include <program_options_helper.h>

using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	char input_format;
	char output_format;

	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("matrix_output_path,m", po::value<string>(&args.matrix_name)->set_name("PATH"),
									"PATH to output the resulting matrices.\n");

	args.optional_args.add_options()
	("input_format,i", po::value<char>(&input_format)->default_value('p')->set_name("FORMAT"),
									"Input FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("output_format,o", po::value<char>(&output_format)->default_value('p')->set_name("FORMAT"),
									"Input FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("threshold,t", po::value<float>(&args.threshold)->default_value(5.0)->set_name("NUMBER"),
									"Matrix entries larger than NUMBER will be discarded. ")
	("header,H",					"Add a header line (matrix name prefixed with '>') "
									"to the matrix output.");

	args.add_std_options();
	po::options_description all_args("");
	all_args.add(mandatory_args).add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);



	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0");
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
				"Takes a matrix from STDIN "
				"and discards values greater than threshold and splits the matrix "
				"using single linkage clustering.\n\n";
		print_usage(cerr, string(*argv), "--matrix_output_path PATH\n[OPTIONS]\n< MATRIX_FILE",
							all_args, exe_description, 80);
		return false;
	}
	check_required_options(vm, mandatory_args);

	args.header		= vm.count("header");


	select_matrix_file_format(input_format, args.input_format, "input format");
	select_matrix_file_format(output_format, args.output_format, "output format");


	args.open_err_log();
	return true;
}







void t_program_args::open_output_files(unsigned index)
{
	open_stream_with_subscript(f_matrix, matrix_name, index, 5);
}

